//
//  ChoseTypeController.m
//  ShowMe
//
//  Created by 123 on 2018/8/27.
//  Copyright © 2018年 nkl. All rights reserved.
//

#import "LeftMenuVC.h"
#import "LoginVC.h"
#import "BaseNavigationController.h"
#import <RESideMenu/RESideMenu.h>
#import "MyViewController.h"
@interface LeftMenuVC ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong) UITableView * tableView;
@property(nonatomic,strong) NSArray * menuArray;
@property(nonatomic,strong) NSArray * menuIconArray;
@property(nonatomic,strong) UIButton* logoutBtn;

@end

@implementation LeftMenuVC

#pragma mark - 懒加载
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.opaque = NO;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.backgroundView = nil;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.bounces = NO;
    }
    return _tableView;
}

-(NSArray *)menuArray{
    if (!_menuArray) {
        _menuArray = @[@"个人",@"通知",@"收藏",@"设置"];
    }
    return _menuArray;
}

-(NSArray *)menuIconArray{
    if (!_menuIconArray) {
        _menuIconArray = @[@"user",@"light",@"collection",@"setting"];
    }
    return _menuIconArray;
}

-(UIButton *)logoutBtn{
    if (!_logoutBtn) {
        _logoutBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _logoutBtn.backgroundColor = ThemeColor;
        [_logoutBtn setTitle:@"退出登录" forState:UIControlStateNormal];
        [_logoutBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_logoutBtn addTarget:self action:@selector(logoutBtnClickEvent) forControlEvents:UIControlEventTouchUpInside];
    }
    return _logoutBtn;
}

#pragma mark - 初始化
-(void)viewDidLoad{
    [super viewDidLoad];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
   
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.top.equalTo(self.view);
        make.width.mas_equalTo(self.view.frame.size.width);
        make.height.mas_equalTo(self.view.frame.size.height);
    }];
   
    [self setUpHeaderAndFooter];
}

-(void)setUpHeaderAndFooter{
    //底
    UIView * footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, __KNewSize(150))];
    footerView.backgroundColor = [UIColor clearColor];
    [footerView addSubview:self.logoutBtn];
    
    [self.logoutBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(footerView);
        make.bottom.equalTo(footerView);
        make.width.mas_equalTo(footerView.frame.size.width/2+30);
        make.height.mas_equalTo(__KNewSize(44));
    }];
    self.tableView.tableFooterView = footerView;
    //头
    UIView * m_headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, __KNewSize(150))];
    m_headerView.backgroundColor = [UIColor clearColor];
    
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor clearColor];
    [m_headerView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(m_headerView);
        make.width.mas_equalTo(self.view.frame.size.width/2+30);
    }];
    
    UIView * lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor whiteColor];
    [bgView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(m_headerView.mas_bottom);
        make.height.mas_equalTo(__KNewSize(1));
        make.width.mas_equalTo(self.view.frame.size.width);
        make.left.equalTo(m_headerView);
    }];
    
    UIButton * headIcon = [[UIButton alloc] init];
    [headIcon setImage:[UIImage imageNamed:@"DeafultHeader"] forState:UIControlStateNormal];
    headIcon.backgroundColor = [UIColor lightGrayColor];
    headIcon.layer.cornerRadius = __KNewSize(32);
    headIcon.layer.masksToBounds = YES;
    [bgView addSubview:headIcon];
    [headIcon mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(m_headerView).offset(__KNewSize(50));
//        make.centerY.equalT12o(m_headerView);
        make.center.equalTo(bgView);
        make.width.height.mas_equalTo(__KNewSize(64));
    }];
    self.tableView.tableHeaderView = m_headerView;
}

-(void)logoutBtnClickEvent{

    [self.sideMenuViewController hideMenuViewController];
    [UserConfig logout];
    
    LoginVC * lgvc = [[LoginVC alloc] init];
    BaseNavigationController * vc = [[BaseNavigationController alloc] initWithRootViewController:lgvc];
    [self presentViewController: vc animated:YES completion:nil];
}



#pragma mark - 代理&数据源

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return __KNewSize(64);
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.menuArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * cellID = @"CellID";
    UITableViewCell * cell  = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:18];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.highlightedTextColor = [UIColor lightGrayColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.selectedBackgroundView = [[UIView alloc] init];
    }
    cell.textLabel.text = self.menuArray[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:self.menuIconArray[indexPath.row]];
 
   
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        //个人
        MyViewController * vc = [[MyViewController alloc] init];
        [self.sideMenuViewController hideMenuViewController];
        BaseNavigationController * baseNav = ( BaseNavigationController *)self.sideMenuViewController.contentViewController;
        [baseNav pushViewController:vc animated:YES];
    }else if (indexPath.row == 1){
        //通知
    }else if (indexPath.row == 2){
        //收藏
    }else if (indexPath.row == 3){
        //设置
    }else{
        return;
    }
}
@end
